from rest_framework.serializers import ModelSerializer  # 序列化器
from .models import *  # 导入所有的模型类
from django.contrib.auth.models import User  # auth自带的用户表
from rest_framework import serializers

# 标签序列化器
class LabelSerializer(ModelSerializer): # 序列化器类
    username = serializers.CharField(source='user.username',read_only=True)
    class Meta:
        model = Label  # 转序列化器
        fields = '__all__'  # 映射展示所有字段
        # 数据校验 数值范围
        extra_kwargs = {
            "user": {"required": False,'write_only':True},
        }

# 老师看题目的序列化器
class TopicSerializer(ModelSerializer): # 序列化器类
    label_name = serializers.CharField(source='label.name',read_only=True)
    user_name = serializers.CharField(source='user.username',read_only=True)

    class Meta:
        model = Topic  # 转序列化器
        exclude = ['is_delete']  # 不想展示的字段


# 学生看题目的序列化器
class TopicStudentSerializer(ModelSerializer):  # 序列化器类
    label_name = serializers.CharField(source='label.name', read_only=True)
    user_name = serializers.CharField(source='user.username', read_only=True)

    class Meta:
        model = Topic  # 转序列化器
        exclude = ['is_delete','answer']  # 不想展示的字段


